<div class="layui-card" id="blogPage">
    <div class="layui-card-header">
        <h2 class="header-title">博客主页</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>博客主页</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <form class="layui-form toolbar" name="queryForm">
            &nbsp;&nbsp;
            <div class="layui-inline">
                创建时间
                <div class="layui-input-inline" style="width: 150px;">
                    <input type="text" name="startDate" class="layui-input" id="startDate" placeholder="开始时间">
                </div>
                <div class="layui-input-inline" style="width: 150px;">
                    <input type="text" name="endDate" class="layui-input" id="endDate" placeholder="结束时间">
                </div>
            </div>
            &nbsp;&nbsp;
            文章类型：
            <select name="artype">
                <option value="">请选择文章类型</option>
            </select>
            &nbsp;&nbsp;
            描述：
            <input name="atitle" class="layui-input search-input enterSubmit" type="text"
                   placeholder="请输入关键字"/>&emsp;
            <button id="article-btn-search" class="layui-btn icon-btn" lay-submit lay-filter="blogPage-query"><i
                    class="layui-icon">&#xe615;</i>搜索
            </button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </form>

        <!-- 数据表格 -->
        <table class="layui-table" id="blogArticle-table" lay-filter="blogArticle-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="blogArticle-state">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="preview">预览</a>
</script>
<script>
    layui.use(['form', 'table', 'util', 'laydate', 'config', 'admin'], function () {
        let form = layui.form,
            table = layui.table,
            config = layui.config,
            layer = layui.layer,
            util = layui.util,
            admin = layui.admin,
            laydate = layui.laydate;
        //日期时间范围
        laydate.render({
            elem: '#blogPage #startDate'
            , type: 'datetime'
        });
        laydate.render({
            elem: '#blogPage #endDate'
            , type: 'datetime'
        });

        // 获取文章状态字典
        let articleState = admin.getTempData('articleState');
        if (!articleState) admin.req('api-forum/article/articleState', {}, (data) => {
            let obj = {};
            for (let item of data) obj[item.mtcode] = item.mtname;
            articleState = obj;
        }, 'GET');

        let url = config.base_server + 'api-forum/article/blogPage';
        let params = {
            access_token: config.getToken().access_token
        };

        let cols = [[
            {type: 'numbers'},
            {field: 'arleid', sort: true, hide: true, title: 'ID', width: 100},
            {field: 'atitle', sort: true, title: '文章标题', align: 'center'},
            {field: 'userid', sort: true, hide: true, align: 'center'},
            {field: 'author', sort: true, title: '作者', align: 'center'},
            {field: 'catgry', sort: true, title: '分类专栏', align: 'center'},
            {field: 'artags', sort: true, title: '标签', align: 'center'},
            {field: 'arhits', sort: true, title: '访问量', align: 'center'},
            {field: 'liknum', sort: true, title: '点赞数', align: 'center'},
            {field: 'cetnum', sort: true, title: '收藏数', align: 'center'},
            {
                field: 'astate', sort: true, title: '状态', align: 'center', templet: (d) => {
                    return articleState[d.astate];
                }
            },
            {
                field: 'cretim', sort: true, align: 'center', templet: (d) => {
                    return util.toDateString(d.cretim);
                }, title: '创建时间'
            },
            {
                field: 'updtim', sort: true, align: 'center', templet: (d) => {
                    return util.toDateString(d.updtim);
                }, title: '修改时间'
            },
            {templet: '#blogArticle-state', width: 120, align: 'center', title: '操作'}
        ]];

        // 渲染表格
        table.render({
            elem: '#blogArticle-table',
            url: url,
            where: params,
            page: true,
            cols: cols
        });

        //显示表单弹窗
        let showPreviewModel = function (data) {
            admin.putTempData('t_article', data);
            layer.open({
                type: 2
                , title: '博文预览'
                , area: ['1710px', '980px']
                , offset: ['-5px', '210px']
                , maxmin: true
                , content: 'pages/forum/articlePreview.html'
                , btn: ['关闭预览']
                , shade: 0.3
                , yes: function () {
                    admin.putTempData('t_article', null);
                    layer.closeAll();
                }
                , success: function (layero) {
                    layer.setTop(layero);
                }, cancel: () => {
                    admin.putTempData('t_article', null);
                }
            });
        };

        // 工具条点击事件
        table.on('tool(blogArticle-table)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            if (layEvent === 'preview') {
                showPreviewModel(data);
            }
        });

        // 填充下拉列表 - 文章类型
        let articleType = $("#blogPage select[name='artype']");
        admin.req('api-forum/article/articleType', {}, (data) => {
            admin.putTempData('articleTypeSelect', data);
            $.each(data, function (index, item) {
                let option = new Option(item.mtname, item.mtcode);
                articleType.append(option);
            });
            form.render();
        }, 'GET');


        // 条件检索
        form.on('submit(blogPage-query)', (data) => {
            table.reload('blogArticle-table', {where: data.field});
            return false;
        });


        // 回车查询
        $('.enterSubmit').keyup((e) => {
            switch (e.keyCode) {
                case 13:
                    $('#article-btn-search').click();
                    break;
            }
        });
    });
</script>